$(function () {
    getUserList();
    getSelectMess();
});
/*重置*/
function resetClick(){
    $("#userName").val("");
    $("#userState option:selected").val("全部");
    getUserList();
}
/*查询*/
function getUserList(init){
    JY.Model.loading();
	$("#ticktTable tbody").html("");
    var pageNum=$("#baseForm .pageNum").val();
    var pageSize=$("#baseForm .pageSize").val();
	var delicateTitle =$("#userName").val().trim();
	var userState=$("#userState option:selected").val();
    if(userState=="全部"){
        userState="";
    }
    $.ajax({
		url : jypath + "/DeclicateAskAnswer/queryArticleAdmin",
		type : "post",
		data:{
            "pageNum":pageNum,
            "pageSize":pageSize,
            "delicateTitle":delicateTitle,
            "state":userState,
        },
		jsonType : "json",
		success : function(data) {
		    var data=data instanceof Object ? data : JSON.parse(data);
		    var html="";
		    console.log(data);
            var list=data.obj.results;
            var pageNum=data.obj.pageNum,
                pageSize=data.obj.pageSize,
                totalRecord=data.obj.totalRecord;
           if(list!=null&&list.length>0){
        	  var leng=(pageNum-1)*pageSize;//计算序号
                for(var i = 0;i<list.length;i++){
                	console.log("a");
                	var results = list[i].ymDelicateArticle;
                	var resu = list[i].ymDelicateAskAnswer;
                	html+="<tr class='tr_color'>";
                    html+="<td class='center ver-m'><label><input type='checkbox' name='ids' value='"+results.delicateArticleId+"' class='ace' /> <span class='lbl'></span></label></td>";
                    html+="<td class='center name'>"+resu.delicateMenuName+"</td>";
                    html+="<td class='center title'>"+results.delicateTitle+"</td>";
                   if(results.state==0){
                        html+="<td class='center'>无效</td>";
                    }else if(results.state==1){
                        html+="<td class='center'>有效</td>";
                    }
                   	html+="<td class='center idids'><input type='hidden' value='"+results.delicateArticleId+"'/></td>";
                	html+="<td class='center idid'><input type='hidden' value='"+resu.delicateMenuId+"'/></td>";
                	html+="<td class='center ididss'><input type='hidden' value='"+results.delicateArticleContent+"'/></td>";
                	html+="</tr>";
            	}
                $("#ticktTable tbody").append(html);
                JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"getUserList");
            }else{
                html+="<tr><td colspan='7' class='center'>没有相关数据</td></tr>";
                $("#ticktTable tbody").append(html);
                $("#baseForm ul").empty();//清空分页
            }
            JY.Model.loadingClose();
		}
	})
}
//获取三级菜单
function getSelectMess(){
    $.ajax({
        url : jypath + "/DeclicateAskAnswer/queryParentMenuAdmin",
        type : "post",
        data:{
            "menuGrade":3,
        },
        jsonType : "json",
        success : function(data) {
        	if(data.obj.length>0){
                var html="";
                for(var i=0;i<data.obj.length;i++){
                     html+="<option value='"+data.obj[i].delicateMenuId+"'>"+data.obj[i].delicateMenuName+"</option>"
                }
                $("#douctorConsultTypeNames").append(html);
             }
         }
    })
}
//查询咨询类型
function getConsulType(){
    $.ajax({
        url : jypath + "/askDoctor/queryYmDouctorConsultType",
        type : "post",
        jsonType : "json",
        success : function(data) {
            if(data.obj.length>0){
                var html="";
                for(var i=0;i<data.obj.length;i++){
                    html+="<option value='"+data.obj[i].douctorConsultTypeId+"'>"+data.obj[i].douctorConsultTypeName+"</option>"
                }
                $("#douctorConsultTypeName").append(html);
            }
        }
    })
}


// 启用禁用
function enAbleClick(type){
	var chks = new Array(); 
     $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
     if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length>0){
        var states="";
        if(type==1){
            //启用
            states=1;
        }else if(type==0){
            //禁用
            states=0;
        }
        JY.Model.message("auForm_edit","修改状态",function(){
            var that = $(this);
            $.ajax({
                url : jypath + "/DeclicateAskAnswer/deleteArticle",
                type : "post",
                data:{
                    "state":states,
                    "ymDelicateArticleId":chks
                },
                jsonType : "json",
                success : function(data) {
                   console.log(data);
                   if(data.res==1){
                       that.dialog("close");
                       JY.Model.info("操作成功!",function(){getUserList();});
                   }else{
                	   that.dialog("close");
                       JY.Model.info("操作失败!",function(){getUserList();});
                   }
                }
            })
        });
    }
}
//新增按钮
function addpro(){
	clearForm();
	JY.Model.edit("addForm_edits","新增文章",function(){
		var a = $("#addForm_edits textarea[name='des']").val();
		 var that=$(this);
		 if(JY.Validate.form("generateForm")){
             $(".masks").removeClass("hide");
			$.ajax({
	            url : jypath + "/DeclicateAskAnswer/insertArticle",
	            type : "post",
	            data:{
	                "ymDelicateMenuId":$("#douctorConsultTypeNames").find("option:selected").val(),
	                "delicateTitle":$("#addForm_edits input[name='articleRealName']").val(),
	                "delicateArticleContent":$("#addForm_edits textarea[name='des']").val(),
	             },
	            jsonType : "json",
	            success : function(data){
	                console.log("新增")
	                if(data.res==1){
	                    that.dialog("close");
                        $(".masks").addClass("hide");
	                    JY.Model.info("新增成功!",function(){getUserList();});
	                }else{
	                	that.dialog("close");
                        $(".masks").addClass("hide");
	                    JY.Model.info("新增失败!",function(){getUserList();});
	                }
	            }
	        })
		 }
	 })
}
//编辑按钮
function editor(){
	var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length==1){
    	var arname = $('#ticktTable input[name="ids"]:checked').parent().parent().siblings('.title').text();
    	var arcon = $('#ticktTable input[name="ids"]:checked').parent().parent().siblings('.ididss').children().val();
    	var arcid = $('#ticktTable input[name="ids"]:checked').parent().parent().siblings('.idid').children().val();
    	$("#addForm_edits select").val(arcid);
        $("#addForm_edits input[name='articleRealName']").val(arname);
        $("#addForm_edits textarea[name='des']").val(arcon);
         JY.Model.edit("addForm_edits","编辑文章",function(){
    		 var that=$(this);
    		 if(JY.Validate.form("generateForm")){
				$.ajax({
		            url : jypath + "/DeclicateAskAnswer/updateArticle",
		            type : "post",
		            data:{
		                "ymDelicateMenuId":$("#douctorConsultTypeNames").find("option:selected").val(),
		                "delicateTitle":$("#addForm_edits input[name='articleRealName']").val(),
		                "delicateArticleContent":$("#addForm_edits textarea[name='des']").val(),
		                "delicateArticleId":chks[0],
		             },
		            jsonType : "json",
		            success : function(data){
		                console.log("编辑")
		                if(data.res==1){
		                    that.dialog("close");
		                    JY.Model.info("编辑成功!",function(){getUserList()});
		                }else{
		                	that.dialog("close");
		                    JY.Model.info("编辑失败!",function(){getUserList()});
		                }
		            }
		        })
			 }
    	 })
        
    }else if(chks.length>1){
        JY.Model.info("一次只能编辑一条信息!");
    }
}

function clearForm(){
    $("#addForm_edits input[name='articleRealName']").val("");
    $("#addForm_edits textarea[name='des']").val("");
    $("#douctorConsultTypeNames").val("");
}


